package hu.szyrt.bug.adapters;

import hu.szyrt.bug.R;
import hu.szyrt.bug.models.Comment;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {

	private List<Comment> items;
	private ViewHolder holder;
	
	public CommentAdapter(List<Comment> items) {
		this.items = items;
	}
	public void changeList(List<Comment> items){
		this.items = items;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() { return items.size(); }
	@Override
	public Object getItem(int position) {return items.get(position);}
	@Override
	public long getItemId(int position) { return position; }
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		Comment item = items.get(position);
		
		if(convertView == null){
			convertView = View.inflate(parent.getContext(), R.layout.comment_item, null);
			holder = new ViewHolder();
			holder.name = (TextView) convertView.findViewById(R.id.name);
			holder.date = (TextView) convertView.findViewById(R.id.date);
			holder.comment = (TextView) convertView.findViewById(R.id.comment);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder)convertView.getTag();
		}
		
		holder.name.setText(item.getName());
		holder.date.setText(item.getReformattedDate() != null ? item.getReformattedDate() : item.getDateString());
		holder.comment.setText(item.getComment());		
		
		return convertView;
	}
	
	static class ViewHolder{
		TextView name, date, comment;
	}

}
